package com.ebt.m.proposal_v2.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.ebt.m.proposal_v2.bean.ViewSize;
import com.ebt.m.proposal_v2.utils.ViewUtils;

/**
 * @author archer.qi
 *         Created on 2017/6/13.
 */
public abstract class DialogBase extends Dialog {

    private View mDialogView;
    private int mDialogWidth;
    private int mDialogHeight;

    protected DialogBase(Context context) {
        super(context);
    }

    protected DialogBase(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected DialogBase(Context context, int themeResId) {
        // Style:{R.style.EBTDialog}
        super(context, themeResId);
    }

    public void setDialog(int width, int height) {
        mDialogWidth = width;
        mDialogHeight = height;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        mDialogView = createView();
        setContentView(mDialogView);
        bindView();
    }

    protected abstract View createView();

    protected abstract void bindView();

    private void setDialogSize() {
        WindowManager.LayoutParams p = getWindow().getAttributes();
        if (mDialogWidth == 0 || mDialogHeight == 0) {
            ViewSize viewSize = ViewUtils.measureView(mDialogView);
            p.width = viewSize.width;
            p.height = viewSize.height;
        } else {
            p.width = mDialogWidth;
            p.height = mDialogHeight;
        }
        p.gravity = Gravity.CENTER;
        getWindow().setAttributes(p);

        getWindow().setBackgroundDrawable(null);
    }

    @Override
    public void show() {
        super.show();
        setDialogSize();
    }

}

